package com.ltsznh.android.heartrate;

import android.app.ActionBar;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.ltsznh.android.heartrate.db.dbo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HistoryActivity extends Activity {

    private AdView adView;

    private ListView ListViewHeartRate;
    private List<String> heartRateList = new ArrayList<String>();
    private SimpleAdapter adapter;
    private ArrayList<HashMap<String, Object>> listItem;
    private int rows = 0;
    private int pages = 0;
    private int thisPage = 0;
    private int pageRows = 8;
    private TextView testViewPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        ActionBar actionBar = this.getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        //************广告开始************//
        // 创建adView。
        adView = new AdView(this);
        adView.setAdUnitId("ca-app-pub-6427167969550621/5778606191");
        adView.setAdSize(AdSize.SMART_BANNER);

//        320x50	标准横幅广告	手机和平板电脑	BANNER
//        320x100	大横幅	手机和平板电脑	LARGE_BANNER
//        300x250	IAB中矩形	手机和平板电脑	MEDIUM_RECTANGLE
//        468x60	IAB全尺寸横幅广告	平板电脑	FULL_BANNER
//        728x90	IAB页首横幅广告	平板电脑	LEADERBOARD
//        查看表格	智能横幅广告	手机和平板电脑	SMART_BANNER

        // 查询LinearLayout，假设其已指定
        // 属性android:id="@+id/mainLayout"。
        LinearLayout layout = (LinearLayout) findViewById(R.id.adMob);

        // 在其中添加adView。
        layout.addView(adView);

//        Bundle bundle = new Bundle();
//        bundle.putString("color_bg", "AAAAFF");//背景颜色
//        bundle.putString("color_bg_top", "FFFFFF");//顶部的渐变背景色
//        bundle.putString("color_border", "FFFFFF");//边框颜色
//        bundle.putString("color_link", "000080");//链接文字颜色
//        bundle.putString("color_text", "808080");//文字颜色
//        bundle.putString("color_url", "008000");//网址颜色

//        AdMobExtras extras = new AdMobExtras(bundle);

        // 启动一般性请求。
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)          // 模拟器
                .addTestDevice("A03EE007D61ACD6F477499B09E1CF201") // 我的Galaxy Nexus测试手机
//                .addNetworkExtras(extras)//设置主题
                .build();

        // 在adView中加载广告请求。
        adView.loadAd(adRequest);
        //************广告结束************//

        testViewPage = (TextView) findViewById(R.id.textViewPage);

        if (listItem == null) listItem = new ArrayList<HashMap<String, Object>>();


        String sqlString = "SELECT count(1) as rows" +
                "  FROM heart_rate_details;";
        dbo.getReadableDatabase(HistoryActivity.this);
        Cursor cursor = dbo.fbSQLite.rawQuery(sqlString, new String[]{});
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            rows = cursor.getInt(0);
            pages = (int) (Math.floor(rows / pageRows));
            Log.v("dddd", rows + " " + pages);
        }
        cursor.close();
        dbo.closeDatabase();


        refresh();
    }

    private void refresh() {
        int from = thisPage * pageRows;
        int show = (rows - from) > pageRows ? pageRows : (rows - from);

        testViewPage.setText((from + 1) + " - " + (from + show) + "  " + (thisPage + 1) + "/" + (pages + 1));

        listItem.clear();
        String sqlString = "SELECT entryDate," +
                "       entryTime," +
                "       hrRate" +
                "  FROM heart_rate_details" +
                "  order by entryDate,entryTime desc " +
                "   limit " + show + " offset " + from + ";";
        Log.v("ddd", sqlString);
        dbo.getReadableDatabase(HistoryActivity.this);
        Cursor cursor = dbo.fbSQLite.rawQuery(sqlString, new String[]{});
        if (cursor.getCount() > 0) {
            HashMap<String, Object> itemMap;
            cursor.moveToFirst();
            do {
                itemMap = new HashMap<String, Object>();
                itemMap.put("entryDate", cursor.getString(cursor.getColumnIndex("entryDate")));
                itemMap.put("entryTime", cursor.getString(cursor.getColumnIndex("entryTime")));
                itemMap.put("hrRate", cursor.getString(cursor.getColumnIndex("hrRate")));

                listItem.add(itemMap);
            } while (cursor.moveToNext());
        }
        cursor.close();
        dbo.closeDatabase();
        //生成适配器的Item和动态数组对应的元素
        SimpleAdapter blottersListItemAdapter = new SimpleAdapter(this, listItem, R.layout.history_item,
                new String[]{"entryDate", "entryTime", "hrRate"},
                new int[]{R.id.textViewDate, R.id.textViewTime,
                        R.id.textViewRate}
        );
        ListViewHeartRate = (ListView) findViewById(R.id.listViewHistory);
        ListViewHeartRate.setAdapter(blottersListItemAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_history, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                Log.d(this.getClass().toString(), "click nothing");
                return super.onOptionsItemSelected(item);
        }
    }


    public void next(MenuItem menuItem) {
        if (thisPage < pages) thisPage++;
        refresh();
    }

    public void previous(MenuItem menuItem) {
        if (thisPage > 0) thisPage--;
        refresh();
    }
}
